﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text.RegularExpressions;

using CE.Global;
using CE.Image;
using CE.CreditCard;

public partial class Admin_credit_card_AddNewCCardDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }
        txtAnualFee.Attributes.Add("OnKeyup", "ControlVisible('" + txtAnualFee.ClientID + "','" + txtAnualFeeDetails.ClientID + "');");
        txtSupplementary.Attributes.Add("OnKeyup", "ControlVisible('" + txtSupplementary.ClientID + "','" + txtSupplementaryDetails.ClientID + "');");
        if (!IsPostBack)
        {
            clearProcess();

            DataTable _dtCompany = General.GetComboItem("ProviderCompany", false);
            ddlProviderCompanyName.DataSource = _dtCompany;
            ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompanyName.DataBind();

            ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));

            if (Request.Params["CCId"] != null)
            {
                Int64 _ccid;
                if (Int64.TryParse(Request.Params["CCId"].ToString(), out _ccid))
                {
                    ShowCCDetails(_ccid);
                }
            }
            
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CreditCardDetails _ccinfo = new CreditCardDetails();
            CreditCardDetailsDB _ccinfodb = new CreditCardDetailsDB();

            _ccinfo.CCId = Convert.ToInt64(lblCCID.Text.Trim());
            _ccinfo.FinancialInstitute = Convert.ToInt32(ddlProviderCompanyName.SelectedValue);
            _ccinfo.CCClass = Convert.ToInt32(ddlCardclass.SelectedValue);
            _ccinfo.ProductName = txtProductname.Text.Trim();

            if ((Uploadcardimage.FileName != null) && (Uploadcardimage.FileName != ""))
            {
                HttpPostedFile myFile = Uploadcardimage.PostedFile;
                int fileSize = myFile.ContentLength;
                MyImage myImage = new MyImage();
                byte[] myData = new byte[fileSize];
                myFile.InputStream.Read(myData, 0, fileSize);
                string strFilename = Path.GetFileName(myFile.FileName);
                string strUploadImagePath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("UploadCardImagePath");
                string uploadPath = Server.MapPath(strUploadImagePath + strFilename);

                _ccinfo.CreditcardImage = strUploadImagePath + strFilename;
                if(File.Exists(uploadPath))
                {
                }
                else
                {
                    myImage.WriteToFile(uploadPath, ref myData);
                }
            }
            else
            {
                _ccinfo.CreditcardImage = "";
            }
            _ccinfo.CreditCardStatus = Convert.ToInt32(ddlCardstatus.SelectedValue);
            _ccinfo.CreditCardType = Convert.ToInt32(ddlCardtype.SelectedValue);

            _ccinfo.AnualIncomeLocal = Convert.ToSingle(txtMinAnnualloc.Text.Trim());
            _ccinfo.AnualIncomeForeigner = Convert.ToSingle(txtMinAnnualfor.Text.Trim());
            _ccinfo.MinimumAge = Convert.ToInt32(ddlMinAgeOfApplicant.Text);
            _ccinfo.MaximumAge = Convert.ToInt32(ddlMaxAgeOfApplicant.Text);

            _ccinfo.BalanceTransfer = Convert.ToSingle(txtBalancetransfer.Text.Trim());
            _ccinfo.BalanceTransDuration = Convert.ToInt32(ddlDurofBalTrans.GetDuration());
            _ccinfo.IntrestRatePurchase = Convert.ToSingle(txtInsratePurchase.Text.Trim());
            _ccinfo.PurchaseRateDuration = Convert.ToInt32(ddlDurofPurchaserate.GetDuration());
            _ccinfo.IntrestRateCash = Convert.ToSingle(txtInterestrateCash.Text.Trim());

            if (Convert.ToInt32(txtAnualFee.Text.Trim()) > 0)
            {
                _ccinfo.AnualFee = txtAnualFee.Text.Trim();
            }
            else if (txtAnualFeeDetails.Text.Trim().Length > 0)
            {
                _ccinfo.AnualFee = txtAnualFeeDetails.Text.Trim();
            }
            else
            {
                _ccinfo.AnualFee = "0";
            }
            if (Convert.ToInt32(txtSupplementary.Text.Trim()) > 0)
            {
                _ccinfo.SupplementFee = txtSupplementary.Text.Trim();
            }
            else if (txtSupplementaryDetails.Text.Trim().Length > 0)
            {
                _ccinfo.SupplementFee = txtSupplementaryDetails.Text.Trim();
            }
            else
            {
                _ccinfo.SupplementFee = "0";
            }
            _ccinfo.LatepayCharge = Convert.ToSingle(txtLatepaymentcharge.Text.Trim());
            _ccinfo.MinimumCharge = Convert.ToSingle(txtMinimumcharge.Text.Trim());
            _ccinfo.MinMonthlyRepay = Convert.ToSingle(txtMinmonthlyrepayment.Text.Trim());
            _ccinfo.CashAdvWdrawCharge = Convert.ToSingle(txtWithdrawalcharge.Text.Trim());
            if (rbtnDollers.Checked == true)
            {
                _ccinfo.Doller = true;
                _ccinfo.Percentage = false;
            }
            else if (rbtnPercentage.Checked == true)
            {
                _ccinfo.Doller = false;
                _ccinfo.Percentage = true;
            }
            _ccinfo.CashAdvFinCharge = Convert.ToSingle(txtFinancecharge.Text.Trim());

            if (rbtnFWyes.Checked == true)
            {
                _ccinfo.FeeWaiver = true;
            }
            else if (rbtnFWno.Checked == true)
            {
                _ccinfo.FeeWaiver = false;
            }
            _ccinfo.FeeWaiverDuration = Convert.ToInt32(ddlDurofFeewaiver.GetDuration());
            if (rbtnWGyes.Checked == true)
            {
                _ccinfo.WelcomeGift = true;
            }
            else if (rbtnWGno.Checked == true)
            {
                _ccinfo.WelcomeGift = false;
            }
            if (rbtnRPyes.Checked == true)
            {
                _ccinfo.RewardPoint = true;
            }
            else if (rbtnRPno.Checked == true)
            {
                _ccinfo.RewardPoint = false;
            }
            _ccinfo.RewardPointExpiry = Convert.ToInt32(ddlRewardspointsExp.GetDurationExpiry());
            _ccinfo.RewardsPrivileges = txtRwdPrivilege.Text.Trim();
            if (rbtnFIyes.Checked == true)
            {
                _ccinfo.FreeInsurance = true;
            }
            else if (rbtnFIno.Checked == true)
            {
                _ccinfo.FreeInsurance = false;
            }
            _ccinfo.IntrestFreeDays = Convert.ToInt32(ddlInsfreedays.GetNoOfDays());
            if (rbtnDMyes.Checked == true)
            {
                _ccinfo.DestinationMiles = true;
            }
            else if (rbtnDMno.Checked == true)
            {
                _ccinfo.DestinationMiles = false;
            }
            _ccinfo.DestMilesDur = Convert.ToInt32(ddlDurofDestinMiles.GetDurationExpiry());
            if (chkCCStatus.Checked == true)
            {
                _ccinfo.Status = true;
            }
            else if (chkCCStatus.Checked == false)
            {
                _ccinfo.Status = false;
            }


            _ccinfo.CCId = _ccinfodb.Insert(_ccinfo);

            if (_ccinfo.CCId > 0)
            {
                //ImageUpload();
                clearProcess();
                lblMessage.Text = "Saved successfully!";
            }
            else
            {
                lblError.Text = "Saving failed!";
            }
        }
    }

    public void ImageUpload()
    {
        if (Uploadcardimage.FileName != "")
        {
            DirectoryInfo thisFolder = new DirectoryInfo(Server.MapPath("cardimages/"));
            if (!thisFolder.Exists)
            {
                thisFolder.Create();
            }
            Uploadcardimage.SaveAs(Server.MapPath("cardimages/" + Uploadcardimage.FileName));
        }
    }

    private void ShowCCDetails(Int64 CCId)
    {
        CreditCardDetails _ccdetails = new CreditCardDetails();
        CreditCardDetailsDB _ccdetailsdb = new CreditCardDetailsDB();
        _ccdetails = _ccdetailsdb.GetCCDetails(CCId);
        if (_ccdetails != null)
        {
            lblCCID.Text = _ccdetails.CCId.ToString();
            ddlProviderCompanyName.SelectedValue = _ccdetails.FinancialInstitute.ToString();
            ddlCardclass.SelectedValue = _ccdetails.CCClass.ToString();
            txtProductname.Text = _ccdetails.ProductName.ToString();
            ddlCardstatus.SelectedValue = _ccdetails.CreditCardStatus.ToString();
            ddlCardtype.SelectedValue = _ccdetails.CreditCardType.ToString();

            txtMinAnnualloc.Text = _ccdetails.AnualIncomeLocal.ToString();
            txtMinAnnualfor.Text = _ccdetails.AnualIncomeForeigner.ToString();
            ddlMinAgeOfApplicant.Text = _ccdetails.MinimumAge.ToString();
            ddlMaxAgeOfApplicant.Text = _ccdetails.MaximumAge.ToString();

            txtBalancetransfer.Text = _ccdetails.BalanceTransfer.ToString();
            ddlDurofBalTrans.Duration = Convert.ToInt32(_ccdetails.BalanceTransDuration.ToString());
            txtInsratePurchase.Text = _ccdetails.IntrestRatePurchase.ToString();
            ddlDurofPurchaserate.Duration = Convert.ToInt32(_ccdetails.PurchaseRateDuration.ToString());
            txtInterestrateCash.Text = _ccdetails.IntrestRateCash.ToString();

            if (_ccdetails.AnualFee.ToString().Length > 0)
            {
                if (CE.Global.General.IsInteger(_ccdetails.AnualFee.ToString()) == true)
                {
                    if (Convert.ToInt32(_ccdetails.AnualFee.ToString()) > 0)
                    {
                        txtAnualFee.Text = _ccdetails.AnualFee.ToString();
                        txtAnualFeeDetails.Text = "";
                    }
                    else
                    {
                        txtAnualFee.Text = "0";
                        txtAnualFeeDetails.Text = "";
                    }
                }
                else
                {
                    txtAnualFee.Text = "0";
                    txtAnualFeeDetails.Text = _ccdetails.AnualFee.ToString();
                }
            }
            else
            {
                txtAnualFee.Text = "0";
                txtAnualFeeDetails.Text = "";
            }
            if (_ccdetails.SupplementFee.ToString().Length > 0)
            {
                if (CE.Global.General.IsInteger(_ccdetails.SupplementFee.ToString()) == true)
                {
                    if (Convert.ToInt32(_ccdetails.SupplementFee.ToString()) > 0)
                    {
                        txtSupplementary.Text = _ccdetails.SupplementFee.ToString();
                        txtSupplementaryDetails.Text = "";
                    }
                    else
                    {
                        txtSupplementary.Text = "0";
                        txtSupplementaryDetails.Text = "";
                    }
                }
                else
                {
                    txtSupplementary.Text = "0";
                    txtSupplementaryDetails.Text = _ccdetails.SupplementFee.ToString();
                }
            }
            else
            {
                txtSupplementary.Text = "0";
                txtSupplementaryDetails.Text = "";
            }

            //txtAnualFee.Text = _ccdetails.AnualFee.ToString();
            //txtSupplementary.Text = _ccdetails.SupplementFee.ToString();
            txtLatepaymentcharge.Text = _ccdetails.LatepayCharge.ToString();
            txtMinimumcharge.Text = _ccdetails.MinimumCharge.ToString();
            txtMinmonthlyrepayment.Text = _ccdetails.MinMonthlyRepay.ToString();
            txtWithdrawalcharge.Text = _ccdetails.CashAdvWdrawCharge.ToString();
            if (_ccdetails.Doller == true)
            {
                rbtnDollers.Checked = true;
                rbtnPercentage.Checked = false;
            }
            else if(_ccdetails.Percentage == true)
            {
                rbtnDollers.Checked = false;
                rbtnPercentage.Checked = true;
            }
            txtFinancecharge.Text = _ccdetails.CashAdvFinCharge.ToString();

            if (_ccdetails.FeeWaiver == true)
            {
                rbtnFWyes.Checked = true;
                rbtnFWno.Checked = false;
            }
            else
            {
                rbtnFWyes.Checked = false;
                rbtnFWno.Checked = true;
            }
            ddlDurofFeewaiver.Duration = Convert.ToInt32(_ccdetails.FeeWaiverDuration.ToString());
            if (_ccdetails.WelcomeGift == true)
            {
                rbtnWGyes.Checked = true;
                rbtnWGno.Checked = false;
            }
            else
            {
                rbtnWGyes.Checked = false;
                rbtnWGno.Checked = true;
            }
            if (_ccdetails.RewardPoint == true)
            {
                rbtnRPyes.Checked = true;
                rbtnRPno.Checked = false;
            }
            else
            {
                rbtnRPyes.Checked = false;
                rbtnRPno.Checked = true;
            }
            ddlRewardspointsExp.DurationExpiry = Convert.ToInt32(_ccdetails.RewardPointExpiry.ToString());
            txtRwdPrivilege.Text = _ccdetails.RewardsPrivileges.ToString();
            if (_ccdetails.FreeInsurance == true)
            {
                rbtnFIyes.Checked = true;
                rbtnFIno.Checked = false;
            }
            else
            {
                rbtnFIyes.Checked = false;
                rbtnFIno.Checked = true;
            }
            ddlInsfreedays.NoOfDays = Convert.ToInt32(_ccdetails.IntrestFreeDays.ToString());
            if (_ccdetails.DestinationMiles == true)
            {
                rbtnDMyes.Checked = true;
                rbtnDMno.Checked = false;
            }
            else
            {
                rbtnDMyes.Checked = false;
                rbtnDMno.Checked = true;
            }
            ddlDurofDestinMiles.DurationExpiry = Convert.ToInt32(_ccdetails.DestMilesDur.ToString());
            if (_ccdetails.Status == true)
            {
                chkCCStatus.Checked = true;
            }
            else
            {
                chkCCStatus.Checked = false;
            }
            btnSubmit.Text = "Update";
            btnClear.Text = "Add New";
        }
    }

    public void clearProcess()
    {
        lblCCID.Text = "0";
        ddlProviderCompanyName.SelectedIndex = -1;
        ddlCardclass.SelectedIndex = 0;
        txtProductname.Text = "";
        ddlCardstatus.SelectedIndex = 0;
        ddlCardtype.SelectedIndex = 0;

        txtMinAnnualloc.Text = "";
        txtMinAnnualfor.Text = "";
        ddlMinAgeOfApplicant.Items.Clear();
        for (int i = 18; i <= 30; i++)
        {
            ddlMinAgeOfApplicant.Items.Add(i.ToString());
        }
        ddlMinAgeOfApplicant.Text = "21";

        ddlMaxAgeOfApplicant.Items.Clear();
        for (int i = 31; i <= 70; i++)
        {
            ddlMaxAgeOfApplicant.Items.Add(i.ToString());
        }
        ddlMaxAgeOfApplicant.Text = "65";

        txtBalancetransfer.Text = "0";
        txtInsratePurchase.Text = "0";
        txtInterestrateCash.Text = "0";

        txtAnualFee.Text = "";
        txtAnualFeeDetails.Text = "";
        txtSupplementary.Text = "";
        txtSupplementaryDetails.Text = "";
        txtLatepaymentcharge.Text = "0";
        txtMinimumcharge.Text = "0";
        txtMinmonthlyrepayment.Text = "0";
        txtWithdrawalcharge.Text = "0";
        rbtnDollers.Checked = true;
        rbtnPercentage.Checked = false;
        txtFinancecharge.Text = "0";

        rbtnFWyes.Checked = false;
        rbtnFWno.Checked = true;
        rbtnWGyes.Checked = false;
        rbtnWGno.Checked = true;
        rbtnRPyes.Checked = false;
        rbtnRPno.Checked = true;
        txtRwdPrivilege.Text = "";
        rbtnFIyes.Checked = false;
        rbtnFIno.Checked = true;
        rbtnDMyes.Checked = false;
        rbtnDMno.Checked = true;
        chkCCStatus.Checked = true;
        lblError.Text = "";
        lblMessage.Text = "";
        btnSubmit.Text = "Submit";
        btnClear.Text = "Clear";
    }

}
